{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_size = 100\n",
    "data_categories = 50\n",
    "data_min = 1\n",
    "data_max = 10\n",
    "X = np.random.randn(data_size,data_categories) # 创建 data_categories 个特征，data_size 个样本\n",
    "w = np.random.randint(data_min,data_max,size=(data_categories,1 )) # 创建 data_categories 个权重参数\n",
    "b = np.random.randint(data_min,data_max,size=1) # 创建截距\n",
    "y = X.dot(w) + b + np.random.randint(data_min,data_max,size=1) # 创建 data_size 个标签\n",
    "X = np.concatenate([X,np.full((data_size,1),fill_value=1)],axis=1) # 添加偏置项\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "epoches = 10000\n",
    "t0= 5\n",
    "t1= 1000\n",
    "\n",
    "# 计算学习率\n",
    "def learning_rate(t):\n",
    "    return t0 / (t + t1)\n",
    "\n",
    "# 计算梯度\n",
    "def gradients(_x, _y, _theta):\n",
    "    m = len(_x)\n",
    "    return _x.T.dot(_x.dot(_theta) - _y)/m   # 梯度下降公式\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 批量下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3]\n",
      " [5]\n",
      " [9]\n",
      " [6]\n",
      " [1]\n",
      " [4]\n",
      " [3]\n",
      " [9]\n",
      " [4]\n",
      " [7]] [6]\n",
      "[[3.35051448]\n",
      " [4.78562625]\n",
      " [8.71927612]\n",
      " [5.91693208]\n",
      " [1.18358803]\n",
      " [4.16820559]\n",
      " [2.64819443]\n",
      " [8.73606429]\n",
      " [4.06582668]\n",
      " [6.805476  ]] [13.91861447]\n"
     ]
    }
   ],
   "source": [
    "theta = np.random.randn(data_categories + 1,1)\n",
    "\n",
    "for epoch in range(epoches):\n",
    "    theta  -= learning_rate(epoch) * gradients(X,y,theta)\n",
    "print(w[:10],b)\n",
    "print(theta[:10],theta[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3]\n",
      " [5]\n",
      " [9]\n",
      " [6]\n",
      " [1]\n",
      " [4]\n",
      " [3]\n",
      " [9]\n",
      " [4]\n",
      " [7]] [6]\n",
      "[[3.03776322]\n",
      " [4.98532897]\n",
      " [8.97114359]\n",
      " [5.99729515]\n",
      " [1.01708936]\n",
      " [4.01117232]\n",
      " [2.97254774]\n",
      " [8.9844589 ]\n",
      " [3.98892025]\n",
      " [6.99176542]] [13.99494974]\n"
     ]
    }
   ],
   "source": [
    "theta = np.random.randn(data_categories + 1,1)\n",
    "index = np.arange(data_size)\n",
    "np.random.shuffle(index)\n",
    "\n",
    "for epoch in range(epoches):\n",
    "    for i in range(data_size):\n",
    "        condition = [i]\n",
    "        grad = gradients(X[condition],y[condition],theta)\n",
    "        theta -= learning_rate(epoch * data_size + i) * grad\n",
    "print(w[:10],b)\n",
    "print(theta[:10],theta[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 随机小批量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3]\n",
      " [5]\n",
      " [9]\n",
      " [6]\n",
      " [1]\n",
      " [4]\n",
      " [3]\n",
      " [9]\n",
      " [4]\n",
      " [7]] [6]\n",
      "[[3.0490555 ]\n",
      " [4.97211464]\n",
      " [8.96483558]\n",
      " [6.00827703]\n",
      " [1.03116464]\n",
      " [4.01120742]\n",
      " [2.94300088]\n",
      " [8.97186512]\n",
      " [3.9857533 ]\n",
      " [6.98231395]] [14.01330014]\n"
     ]
    }
   ],
   "source": [
    "theta = np.random.randn(data_categories + 1,1)\n",
    "index = np.arange(data_size)\n",
    "np.random.shuffle(index)\n",
    "bitch_size = 8\n",
    "bitch_count = data_size // bitch_size\n",
    "\n",
    "for epoch in range(epoches):\n",
    "    for i in range(bitch_count):\n",
    "        condition = index[i*bitch_size:(i+1)*bitch_size]\n",
    "        grad = gradients(X[condition],y[condition],theta)\n",
    "        theta -= learning_rate(epoch * bitch_size + i) * grad\n",
    "print(w[:10],b)\n",
    "print(theta[:10],theta[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.2358111  -0.3034776   1.        ]\n",
      " [ 0.82326839 -0.86426059  1.        ]\n",
      " [ 1.75594163  0.30697061  1.        ]\n",
      " [-0.59341513  0.02254386  1.        ]\n",
      " [ 0.49112247  1.34901555  1.        ]\n",
      " [-0.99815931 -0.14601719  1.        ]\n",
      " [ 0.15205759 -0.64360363  1.        ]\n",
      " [ 1.35590826 -0.61258307  1.        ]\n",
      " [-0.96125657 -0.17286489  1.        ]\n",
      " [ 2.91021976 -0.2216472   1.        ]\n",
      " [ 1.27852713  2.52786763  1.        ]\n",
      " [ 0.46678226  0.40665837  1.        ]\n",
      " [-0.26178445  0.83412239  1.        ]\n",
      " [-0.79032048  1.08216481  1.        ]\n",
      " [ 1.38036568  0.13359838  1.        ]\n",
      " [-0.93555032 -1.34733002  1.        ]\n",
      " [-0.03237225 -0.91979904  1.        ]\n",
      " [-1.98553068 -0.69576818  1.        ]\n",
      " [ 0.21604    -0.54043738  1.        ]\n",
      " [-0.95503259 -0.33676349  1.        ]\n",
      " [-0.24018962 -0.50843002  1.        ]\n",
      " [ 0.75022916  0.50932555  1.        ]\n",
      " [-0.03130587 -1.22645896  1.        ]\n",
      " [ 1.11471897  0.95130797  1.        ]\n",
      " [-0.49657749 -1.19212454  1.        ]\n",
      " [ 0.52404462  1.0044139   1.        ]\n",
      " [-0.41499799 -1.15388971  1.        ]\n",
      " [-1.57017569  1.07723051  1.        ]\n",
      " [-0.8191167   1.83342836  1.        ]\n",
      " [ 1.41885893  0.33808357  1.        ]\n",
      " [ 0.3309697   1.39224715  1.        ]\n",
      " [ 0.92329022  0.74488709  1.        ]\n",
      " [-1.24968576 -0.51481583  1.        ]\n",
      " [-0.63287111 -1.23035576  1.        ]\n",
      " [-0.44887702 -1.0504592   1.        ]\n",
      " [ 1.02335212  0.07504275  1.        ]\n",
      " [ 0.40421598  0.09866309  1.        ]\n",
      " [ 0.44834792 -0.51908619  1.        ]\n",
      " [ 0.23922362  0.60894474  1.        ]\n",
      " [ 0.84293964  2.38413703  1.        ]\n",
      " [-0.92146881 -3.18433266  1.        ]\n",
      " [ 0.80301459 -0.84378842  1.        ]\n",
      " [ 0.10635268  1.23896581  1.        ]\n",
      " [-0.60476502 -1.15049201  1.        ]\n",
      " [-1.01888558  0.34657659  1.        ]\n",
      " [-0.81692381  1.68055083  1.        ]\n",
      " [-0.52596553 -0.31619855  1.        ]\n",
      " [ 1.50536143 -0.7349643   1.        ]\n",
      " [-0.85962882  0.08277688  1.        ]\n",
      " [ 0.91641951  1.73419354  1.        ]\n",
      " [ 1.23162875 -1.52397752  1.        ]\n",
      " [ 0.65808325  0.10113057  1.        ]\n",
      " [-0.79578674 -1.15392414  1.        ]\n",
      " [ 0.33999949  0.63738578  1.        ]\n",
      " [-1.00081675  1.96496109  1.        ]\n",
      " [ 1.87598438  1.04717439  1.        ]\n",
      " [ 0.37477167 -0.27458058  1.        ]\n",
      " [-0.6190499  -1.0512532   1.        ]\n",
      " [ 0.676056   -0.58062865  1.        ]\n",
      " [-0.76575056 -0.00894053  1.        ]\n",
      " [-1.30972497 -0.5152191   1.        ]\n",
      " [-0.7035521  -0.66174748  1.        ]\n",
      " [-0.24572176 -1.00141362  1.        ]\n",
      " [-0.07559709 -0.31115387  1.        ]\n",
      " [-0.08415257  1.59437682  1.        ]\n",
      " [ 1.21064056 -2.47217321  1.        ]\n",
      " [ 0.06349227 -0.53221929  1.        ]\n",
      " [-1.32618217  2.38230413  1.        ]\n",
      " [-0.24513013 -0.59635398  1.        ]\n",
      " [ 0.76890538  0.06240283  1.        ]\n",
      " [ 0.30107246  0.46886624  1.        ]\n",
      " [ 0.18719962  0.87880001  1.        ]\n",
      " [ 1.44115892  0.23548477  1.        ]\n",
      " [ 0.07487272  1.74802549  1.        ]\n",
      " [ 0.80182303 -0.42343503  1.        ]\n",
      " [ 0.62805325 -1.02483952  1.        ]\n",
      " [ 1.15948029  0.68032206  1.        ]\n",
      " [ 1.32859088 -1.40804249  1.        ]\n",
      " [ 0.03227703  0.63616225  1.        ]\n",
      " [-0.24363134  0.95754383  1.        ]\n",
      " [-0.4675001  -1.18257655  1.        ]\n",
      " [ 0.77885376  0.40242072  1.        ]\n",
      " [ 0.29556346  0.11242905  1.        ]\n",
      " [-0.45085744 -0.36046671  1.        ]\n",
      " [ 0.87629378 -1.39191001  1.        ]\n",
      " [ 0.04746654 -0.47913369  1.        ]\n",
      " [ 0.28942036 -1.17233891  1.        ]\n",
      " [-1.0405384  -0.68750018  1.        ]\n",
      " [ 2.35510641  0.06453954  1.        ]\n",
      " [ 2.09864471 -0.47041377  1.        ]\n",
      " [-0.864134    1.31766794  1.        ]\n",
      " [ 0.87103161 -0.08878711  1.        ]\n",
      " [-0.32294457  1.45195172  1.        ]\n",
      " [-0.29500261  1.84208969  1.        ]\n",
      " [ 1.47677616 -1.27961882  1.        ]\n",
      " [-0.2268207   0.32935729  1.        ]\n",
      " [ 1.38275052 -0.38168504  1.        ]\n",
      " [-0.24794668 -0.83084481  1.        ]\n",
      " [ 0.13396967 -0.36736682  1.        ]\n",
      " [-0.906463    1.43568677  1.        ]] [[240.45912587]\n",
      " [197.70980133]\n",
      " [292.03915013]\n",
      " [118.40857865]\n",
      " [227.24606846]\n",
      " [ 85.62627663]\n",
      " [155.34960189]\n",
      " [241.56749269]\n",
      " [ 87.6020264 ]\n",
      " [361.30607032]\n",
      " [311.44424921]\n",
      " [202.90134103]\n",
      " [161.43224154]\n",
      " [129.85920116]\n",
      " [261.21232444]\n",
      " [ 61.24000662]\n",
      " [135.62639347]\n",
      " [  2.32888546]\n",
      " [162.36834288]\n",
      " [ 84.11036219]\n",
      " [130.74421609]\n",
      " [225.49008374]\n",
      " [128.34226792]\n",
      " [261.97643777]\n",
      " [ 96.13200915]\n",
      " [221.31310151]\n",
      " [102.84178992]\n",
      " [ 74.37105825]\n",
      " [145.8449953 ]\n",
      " [268.85298953]\n",
      " [216.91278035]\n",
      " [243.43089566]\n",
      " [ 58.91673125]\n",
      " [ 85.53761306]\n",
      " [102.91871093]\n",
      " [234.45902675]\n",
      " [191.06724879]\n",
      " [179.37463376]\n",
      " [191.59955116]\n",
      " [277.06800304]\n",
      " [ 18.15173101]\n",
      " [196.7631138 ]\n",
      " [197.28621942]\n",
      " [ 89.44987502]\n",
      " [ 95.97696201]\n",
      " [142.33162962]\n",
      " [115.06768212]\n",
      " [249.24151812]\n",
      " [100.95299921]\n",
      " [266.68643001]\n",
      " [210.87018111]\n",
      " [209.15104448]\n",
      " [ 75.80496225]\n",
      " [199.43722274]\n",
      " [136.10107724]\n",
      " [318.32707648]\n",
      " [180.01885465]\n",
      " [ 90.8173802 ]\n",
      " [194.06488799]\n",
      " [105.41713789]\n",
      " [ 54.64426859]\n",
      " [ 94.16586114]\n",
      " [118.51982783]\n",
      " [147.16491387]\n",
      " [192.29021146]\n",
      " [186.62332259]\n",
      " [151.73468794]\n",
      " [123.01636512]\n",
      " [128.28326529]\n",
      " [216.08994959]\n",
      " [192.62893482]\n",
      " [194.38237302]\n",
      " [267.97391799]\n",
      " [207.26857527]\n",
      " [206.7669941 ]\n",
      " [179.99563231]\n",
      " [258.65082981]\n",
      " [220.53693295]\n",
      " [177.5595633 ]\n",
      " [165.68322703]\n",
      " [ 98.42565559]\n",
      " [224.95671405]\n",
      " [183.68330301]\n",
      " [119.33792065]\n",
      " [188.81101802]\n",
      " [151.87091608]\n",
      " [152.41271207]\n",
      " [ 69.62176953]\n",
      " [328.76150385]\n",
      " [297.71384406]\n",
      " [130.27051659]\n",
      " [219.71235356]\n",
      " [171.91777654]\n",
      " [183.26496686]\n",
      " [234.1402557 ]\n",
      " [151.80030502]\n",
      " [249.01484593]\n",
      " [122.45551034]\n",
      " [160.69504325]\n",
      " [130.09760955]]\n"
     ]
    }
   ],
   "source": [
    "print(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
